package com.hmall.service;

import com.hdmall.business.common.common.ServerResponse;
import com.hmall.mapper.MallHistoryMapper;
import com.hmall.mapper.MallProductMapper;
import com.hmall.vo.HistoryVo;
import com.hmall.model.MallProduct;
import com.hmall.model.SysUser;
import com.hmall.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @Author zhoutao
 * @Date 2018/11/8 16:28
 * @Param
 * @return
 * @Description // 历史
 */
@Service
public class HistoryService {

    private MallHistoryMapper mallHistoryMapper;

    private MallProductMapper mallProductMapper;

    @Autowired
    public HistoryService(MallHistoryMapper mallHistoryMapper, MallProductMapper mallProductMapper) {
        this.mallHistoryMapper = mallHistoryMapper;
        this.mallProductMapper = mallProductMapper;
    }

    public ServerResponse<PageVo<HistoryVo>> viewHistory(Integer userId, int pageNum, int pageSize) {

        int pageStart = pageSize * (pageNum - 1);
        int total = mallHistoryMapper.selectViewCount(userId);
        List<Integer> v = mallHistoryMapper.selectView(userId, pageStart, pageSize);

        List<MallProduct> m;
        //产品的历史
        PageVo<HistoryVo> pageVo;
        if(v.size()!= 0) {
            m = mallProductMapper.selectProductList(v);

            List<HistoryVo> historyVoList = new ArrayList<>();

            System.out.println(m.size());
            for (int i = 0; i < m.size(); i++) {
                historyVoList.add(new HistoryVo(v.get(i), m.get(i).getId(), m.get(i).getCategoryId(), m.get(i).getName(),
                        m.get(i).getSubtitle(), m.get(i).getMainImage(), m.get(i).getPrice(), m.get(i).getStock(), m.get(i).getStatus(),
                        m.get(i).getCreateTime(), m.get(i).getUpdateTime(), m.get(i).getProductno(), m.get(i).getShopid(), m.get(i).getUprice(),
                        m.get(i).getCommentNum(), m.get(i).getSpecial(), m.get(i).getSearchHot(), m.get(i).getIsnew(), m.get(i).getSubImages(),
                        m.get(i).getDetail(), m.get(i).getContent()));

            }
            System.out.println(historyVoList);

           pageVo = new PageVo<>(pageNum, pageSize, total, historyVoList);
        }else {
            pageVo = new PageVo<>(pageNum, pageSize, 0, null);
        }


        return ServerResponse.createBySuccess(pageVo);
    }

    public ServerResponse deleteHistory(Integer productId) {

        mallHistoryMapper.deleteHistory(productId);
        return ServerResponse.createBySuccess();
    }

    public ServerResponse deleteAllHistory(SysUser user) {

        mallHistoryMapper.deleteAllHistory(user.getUserId());
        return ServerResponse.createBySuccess();
    }
}
